﻿using StudySystem.MainLogic.FormControl;
using StudySystem.SQLServer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudySystem.MainLogic.Items
{
    public class UserInformation
    {
        private string _userID;
        private string _userName;

        public string UserID { get => _userID; set => _userID = value; }
        public string UserName { get => _userName; set => _userName = value; }


        public void SetUserInfo(string userID, string userName)
        {
            UserID = userID;
            UserName = userName;
        }


        /// <summary>
        /// 检查用户信息，正确则保存到当前对象
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool CheckID(string userID, string password)
        {
            bool ret = false;
            //检查DB
            ret = DBForUser.CheckID(userID, password, this);
            return ret;
        }

        public void UserRightEnter()
        {
            UserManagement.Clear();//清空内存信息
            UserManagement.user = this;//内存记录当前用户
            DBForSubject.GetSubjects(UserID, ref UserManagement.ds);
            DBForQuestion.GetQuestion(ref UserManagement.ds);
            UserManagement.listQuestions = ClassQuestion.SortQuestion(UserManagement.ds.Tables["QuestionTable"]);
        }

        /// <summary>
        /// 将ds中的SubjectTable转成list
        /// </summary>
        /// <returns></returns>
        public List<ClassSubject> GetSubjectsOfUser()
        {
            DataTable dt = UserManagement.ds.Tables["SubjectTable"];
            List<ClassSubject> subjects = new List<ClassSubject>();
            foreach (DataRow item in dt.Rows)
            {
                ClassSubject subject = new ClassSubject(item);
                subjects.Add(subject);
            }
            return subjects;
        }
    }
}
